﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ItemCountExpanderConverter.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   the ItemCountExpander Converter is used to automatically expand a List of Objects if the ListCount is
//   lower than a provided value, the usage is intended for the initial population of a just opened List
//   (one-time-binding)
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.Controls.ConvertersAndTemplateSelectors
{
    using System;
    using System.Globalization;
    using System.Windows.Controls;
    using System.Windows.Data;

    /// <summary>
    ///   the ItemCountExpander Converter is used to automatically expand a List of Objects if the ListCount is
    ///   lower than a provided value, the usage is intended for the initial population of a just opened List
    ///   (one-time-binding)
    /// </summary>
    public class ItemCountExpanderConverter : IValueConverter
    {
        #region Public Methods and Operators

        /// <summary>
        /// int to bool converter, where int is the Item count and bool is the expanded value
        /// </summary>
        /// <param name="value">
        /// Number of items in the list 
        /// </param>
        /// <param name="targetType">
        /// should be Bool 
        /// </param>
        /// <param name="parameter">
        /// maimal number of items until the expansion is set to true 
        /// </param>
        /// <param name="culture">
        /// </param>
        /// <returns>
        /// true, if expaned, false otherwise 
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            
            if (value is int)
            {
                var itemCount = (int)value;
                var maxItems = 10;
                if (parameter is string)
                {
                    int.TryParse((string)parameter, out maxItems);
                }

                if (itemCount > maxItems)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// nothing to do here
        /// </summary>
        /// <param name="value">
        /// </param>
        /// <param name="targetType">
        /// </param>
        /// <param name="parameter">
        /// </param>
        /// <param name="culture">
        /// </param>
        /// <returns>
        /// The System.Object. 
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}